

#include <stdio.h>

#include "iniparser.h"

typedef struct
{
    char *id;
    char *key;
} CONF_DEVICE_T;

typedef struct
{
    char *host;
    char *path;
    char *time_start;
    char *time_end;
    int port;
    int interval;
} CONFIG_SERVICE_T;

typedef struct
{
    double longitude;
    double latitude;
} CONFIG_GPS_T;

typedef struct
{
    CONF_DEVICE_T device;
    CONFIG_SERVICE_T server;
    CONFIG_GPS_T gps;
} CONFIG_T;

static CONFIG_T configs = {0};

static const char *CONFIG_FILENAME = "test_conf.ini";

static const char *CONFIG_KEY_DEVICE_ID = "device:id";
static const char *CONFIG_KEY_DEVICE_KEY = "device:key";

static const char *CONFIG_KEY_SERVICE_HOST = "service:host";
static const char *CONFIG_KEY_SERVICE_PATH = "service:path";
static const char *CONFIG_KEY_SERVICE_PORT = "service:port";
static const char *CONFIG_KEY_SERVICE_INTERVAL = "service:interval";
static const char *CONFIG_KEY_SERVICE_TIMESTART = "service:time_start";
static const char *CONFIG_KEY_SERVICE_TIMEEND = "service:time_end";

static const char *CONFIG_KEY_GPS_LONGITUDE = "gps_info:longitude";
static const char *CONFIG_KEY_GPS_LATITUDE = "gps_info:latitude";

static const char *DEFAULT_CONFIG_VALUE_DEVICE_ID = "0000000000000";
static const char *DEFAULT_CONFIG_VALUE_DEVICE_KEY = "00000000";

static const char *DEFAULT_CONFIG_VALUE_SERVICE_HOST = "115.29.109.104";
static const char *DEFAULT_CONFIG_VALUE_SERVICE_PATH = "/test";
static const int DEFAULT_CONFIG_VALUE_SERVICE_PORT = 6525;
static const int DEFAULT_CONFIG_VALUE_SERVICE_INTERVAL = 60;
static const char *DEFAULT_CONFIG_VALUE_SERVICE_TIMESTART = "08:00";
static const char *DEFAULT_CONFIG_VALUE_SERVICE_TIMEEND = "20:00";

static const double DEFAULT_CONFIG_VALUE_GPS_LONGITUDE = 0.010000;
static const double DEFAULT_CONFIG_VALUE_GPS_LATITUDE = 0.010000;

static void safe_free(void *ptr)
{
    if (ptr != NULL)
    {
        free(ptr);
        ptr = NULL;
    }
}

void configs_init(void)
{
    dictionary *configFile = iniparser_load(CONFIG_FILENAME);
    if (configFile == NULL)
    {
        fprintf(stderr, "ini file load failure!");
        exit(EXIT_FAILURE);
    }

    configs.device.id = strdup(iniparser_getstring(configFile, CONFIG_KEY_DEVICE_ID, DEFAULT_CONFIG_VALUE_DEVICE_ID));
    configs.device.key = strdup(iniparser_getstring(configFile, CONFIG_KEY_DEVICE_KEY, DEFAULT_CONFIG_VALUE_DEVICE_KEY));

    configs.server.host = strdup(iniparser_getstring(configFile, CONFIG_KEY_SERVICE_HOST, DEFAULT_CONFIG_VALUE_SERVICE_HOST));
    configs.server.path = strdup(iniparser_getstring(configFile, CONFIG_KEY_SERVICE_PATH, DEFAULT_CONFIG_VALUE_SERVICE_PATH));
    configs.server.time_start = strdup(iniparser_getstring(configFile, CONFIG_KEY_SERVICE_TIMESTART, DEFAULT_CONFIG_VALUE_SERVICE_TIMESTART));
    configs.server.time_end = strdup(iniparser_getstring(configFile, CONFIG_KEY_SERVICE_TIMEEND, DEFAULT_CONFIG_VALUE_SERVICE_TIMEEND));
    configs.server.port = iniparser_getint(configFile, CONFIG_KEY_SERVICE_PORT, DEFAULT_CONFIG_VALUE_SERVICE_PORT);
    configs.server.interval = iniparser_getint(configFile, CONFIG_KEY_SERVICE_INTERVAL, DEFAULT_CONFIG_VALUE_SERVICE_INTERVAL);

    configs.gps.longitude = iniparser_getdouble(configFile, CONFIG_KEY_GPS_LONGITUDE, DEFAULT_CONFIG_VALUE_GPS_LONGITUDE);
    configs.gps.latitude = iniparser_getdouble(configFile, CONFIG_KEY_GPS_LATITUDE, DEFAULT_CONFIG_VALUE_GPS_LATITUDE);
}

void configs_destory(void *ptr)
{
    safe_free(configs.device.id);
    safe_free(configs.device.key);

    safe_free(configs.server.host);
    safe_free(configs.server.path);
    safe_free(configs.server.time_start);
    safe_free(configs.server.time_end);
}

void configs_show(void)
{
    printf("device id is %s\n", configs.device.id);
    printf("device key is %s\n", configs.device.key);

    printf("server host is %s\n", configs.server.host);
    printf("server path is %s\n", configs.server.path);
    printf("server start time is %s\n", configs.server.time_start);
    printf("server end time is %s\n", configs.server.time_end);
    printf("server port is %d\n", configs.server.port);
    printf("server interval is %d\n", configs.server.interval);

    printf("gps longitude is %lf\n", configs.gps.longitude);
    printf("gps latitude is %lf\n", configs.gps.latitude);
}

char *configs_get_device_id(void)
{
    return configs.device.id;
}

char *configs_getdevice_key(void)
{
    return configs.device.key;
}

char *configs_get_server_host(void)
{
    return configs.server.host;
}

char *configs_get_server_path(void)
{
    return configs.server.path;
}

char *configs_get_server_time_start(void)
{
    return configs.server.time_start;
}

char *configs_get_server_time_end(void)
{
    return configs.server.time_end;
}

int configs_get_server_port(void)
{
    return configs.server.port;
}

int configs_get_server_interval(void)
{
    return configs.server.interval;
}

double configs_get_gps_longitude(void)
{
    return configs.gps.longitude;
}

double configs_get_gps_latitude(void)
{
    return configs.gps.latitude;
}